#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class FileDownProvider : CY.GFive.Core.Providers.DALProvider.IFileDownProvider
	{
		#region Sql
		private static readonly string SqlSelectFileDown = "SELECT [Id], [DownLoader],[Date],[Des],[FileItemCode] FROM [FileDown] WHERE  [Id]=@Id";

		private static readonly string SqlInsertFileDown = "INSERT INTO [FileDown]([DownLoader],[Date],[Des],[FileItemCode]) values(@DownLoader,@Date,@Des,@FileItemCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateFileDown = "UPDATE [FileDown] SET [DownLoader] = @DownLoader,[Date] = @Date,[Des] = @Des,[FileItemCode] = @FileItemCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteFileDown = "DELETE [FileDown] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllFileDown = "SELECT [Id], [DownLoader],[Date],[Des],[FileItemCode] FROM [FileDown]";
		#endregion

		#region IFileDownProvider Members
		public Core.Business.FileDown Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectFileDown);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.FileDown fileDown = new Core.Business.FileDown();

				if (!reader.IsDBNull(0)) fileDown.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) fileDown.DownLoader = reader.GetString(1);
				if (!reader.IsDBNull(2)) fileDown.Date = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) fileDown.Des = reader.GetString(3);
				if (!reader.IsDBNull(4)) fileDown.FileItemCode = reader.GetString(4);

				return fileDown;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.FileDown fileDown)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DownLoader", SqlDbType.NVarChar, fileDown.DownLoader);
			sql.AddParameter("@Date", SqlDbType.DateTime, fileDown.Date);
			sql.AddParameter("@Des", SqlDbType.NVarChar, fileDown.Des);
			sql.AddParameter("@FileItemCode", SqlDbType.NVarChar, fileDown.FileItemCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertFileDown);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) fileDown.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.FileDown fileDown)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DownLoader", SqlDbType.NVarChar, fileDown.DownLoader);
			sql.AddParameter("@Date", SqlDbType.DateTime, fileDown.Date);
			sql.AddParameter("@Des", SqlDbType.NVarChar, fileDown.Des);
			sql.AddParameter("@FileItemCode", SqlDbType.NVarChar, fileDown.FileItemCode);
			sql.AddParameter("@Id", SqlDbType.Int, fileDown.Id);
			sql.ExecuteSql(SqlUpdateFileDown);
		}

		public void Delete(Core.Business.FileDown fileDown)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, fileDown.Id);
			sql.ExecuteSql(SqlDeleteFileDown);
		}

		public IList<Core.Business.FileDown> GetAllFileDown()
		{
			IList<Core.Business.FileDown> fileDownlist = new List<Core.Business.FileDown>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllFileDown);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.FileDown fileDown = new Core.Business.FileDown();

					if (!reader.IsDBNull(0)) fileDown.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) fileDown.DownLoader = reader.GetString(1);
					if (!reader.IsDBNull(2)) fileDown.Date = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) fileDown.Des = reader.GetString(3);
					if (!reader.IsDBNull(4)) fileDown.FileItemCode = reader.GetString(4);

					fileDown.MarkOld();
					fileDownlist.Add(fileDown);
				}
				reader.Close();
			}
			return fileDownlist;
		}
		#endregion
	}
}
